﻿using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
	//Public instance fields, can be set in the inspector.
	public float MoveSpeed;
	public float JumpHeight;
	
	//Public, hidden instance fields, cannot be set in inspector, but seen by other classes.
	[HideInInspector]
	public bool CanJump;
	[HideInInspector]
	public bool CanMoveVertical;
	[HideInInspector]
	public bool HasKey {get; set;}
	[HideInInspector]
	public bool HasWildcard {get; set;}

	//Private instance fields.
	private float Vertical;
	
		// Update is called once per frame
	void Start (){
		Vertical = 0f;
		CanMoveVertical = false;
		HasKey = false;
	}

	void Update(){
				//Basic Movement on the Horizontal Axis (LEFT/RIGHT)
		float Horizontal = Input.GetAxis ("Horizontal");
		
		if(CanMoveVertical){
			Vertical = Input.GetAxis("Vertical");
			rigidbody2D.isKinematic = true;
		}else{
			Vertical = 0f;
			rigidbody2D.isKinematic = false;
		}
		
		Vector3 Movement = new Vector3 (Horizontal, Vertical, 0f);
		transform.position += Movement * MoveSpeed * Time.deltaTime;

				//Basic Code for Jumping 
		if (Input.GetButtonDown ("Jump")) {
			if (CanJump) {
				rigidbody2D.AddForce (Vector3.up * JumpHeight * 10);
			}
		}
	}

	void OnCollisionEnter2D (Collision2D collision)
	{
				//If player is "touching" the ground, he is allowed to jump.
		if (collision.gameObject.tag.Equals ("Ground")) {
			CanJump = true;
		}
	}

	void OnCollisionExit2D (Collision2D collision)
	{
		//If player stops touching the ground, he is not allowed to jump again.
		if (collision.gameObject.tag.Equals ("Ground")) {
			CanJump = false;
		}
	}
}
